﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace AppSocketControl.NOTUSE
{
    public class SocketHandle
    {
        /// <summary>
        /// 获取正确的端口号
        /// </summary>
        /// <param name="port">端口号</param>
        /// <returns></returns>
        protected int GetValidPort(string port)
        {
            int lport;
            //测试端口号是否有效  
            try
            {
                //是否为空  
                if (port == "")
                {
                    throw new ArgumentException("端口号无效，不能启动DUP");
                }
                lport = System.Convert.ToInt32(port);
            }
            catch (Exception e)
            {
                //ArgumentException,   
                //FormatException,   
                //OverflowException  
                Console.WriteLine("无效的端口号：" + e.ToString());
                //this.tbMsg.AppendText("无效的端口号：" + e.ToString() + "\n");
                return -1;
            }
            return lport;
        }

        /// <summary>
        /// 获取正确的IP
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <returns></returns>
        protected IPAddress GetValidIP(string ip)
        {
            IPAddress lip = null;
            //测试IP是否有效  
            try
            {
                //是否为空  
                if (!IPAddress.TryParse(ip, out lip))
                {
                    throw new ArgumentException(
                        "IP无效，不能启动DUP");
                }
            }
            catch (Exception e)
            {
                //ArgumentException,   
                //FormatException,   
                //OverflowException  
                Console.WriteLine("无效的IP：" + e.ToString());
                //this.tbMsg.AppendText("无效的IP：" + e.ToString() + "\n");
                return null;
            }
            return lip;
        }
    }
}
